/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Article_category接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Article_categoryRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Article_categoryRepository:BaseRepository<Article_category,Int32>, IArticle_categoryRepository
    {
        public Article_categoryRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }
        /// <summary>
        /// 增加、修改
        /// </summary>
        /// <param name="models"></param>
        /// <param name="category_id"></param>
        /// <returns></returns>
        public int AddValue(List<Article_category_spec> models, int category_id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in models)
                {
                    if (item.id > 0)
                    {
                        sb.Append("update [" + nameof(Article_category_spec) + "] set category_id=" + item.category_id + ",spec_id=" + item.spec_id + " where id=" + item.id);
                    }
                    else
                    {
                        sb.Append("insert into [" + nameof(Article_category_spec) + "](category_id,spec_id) values (" + category_id + "," + item.spec_id + ");");
                    }
                }
                return _dbConnection.Execute(sb.ToString());
            }
        }

        public bool DeleteAll(int id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("delete from [" + nameof(Article_category_spec) + "] where category_id=@category_id;delete from [" + nameof(Article_category) + "] where class_list like '%," + id + ",%'", new { category_id = id }) > 0;
            }
        }

        public bool DeleteValue(int id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("delete from [" + nameof(Article_category_spec) + "] where category_id=@id", new { id = id }) > 0;
            }
        }
        public bool UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Article_category) + "] set " + strValue + " where id=@id", new { id = id }) > 0;
            }
        }
    }
}